package com.ks.component.ipimage.fragment

import android.graphics.Color
import android.graphics.PixelFormat
import android.os.Bundle
import android.view.*
import android.widget.FrameLayout
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import com.richard.base.view.gdxView.adapter.GdxAdapter
import com.richard.base.R
import kotlinx.android.synthetic.main.base_widget_gdx_fragment.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/9/21     2:59 PM
 * 用途:
 ***************************************
 */
class GdxFragment : AndroidFragmentApplication() {

    private val EXTRA_PATH_ATLAS = "atlas_path"
    private val EXTRA_PATH_JSON = "json_path"

    private var gdxAdapter: GdxAdapter? = null
    private var gdxView: View? = null

    lateinit var atlasPath: String
    lateinit var jsonPath: String

    private val exec: ExecutorService = Executors.newSingleThreadExecutor()
    private val forcePause = Runnable {
        try {
            Thread.sleep(100)
        } catch (e: InterruptedException) {
        }
        graphics.onDrawFrame(null)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.base_widget_gdx_fragment, container, false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            atlasPath = it.getString(EXTRA_PATH_ATLAS)!!
            jsonPath = it.getString(EXTRA_PATH_JSON)!!
//            width = it.getInt(EXTRA_WIDTHD, 0)
//            height = it.getInt(EXTRA_HEIGHT, 0)
//            if (width == 0 || height == 0) {
//                throw IllegalArgumentException("必须传入宽高，否则无法展示")
//            }
        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        context?.let {
            gdxAdapter = GdxAdapter(it, atlasPath, jsonPath)
        }

        val cfg = AndroidApplicationConfiguration()
        cfg.r = 8.also { cfg.a = it }.also { cfg.b = it }.also { cfg.g = it }

        gdxView = initializeForView(gdxAdapter, cfg)
        if (gdxView is SurfaceView) {
            val glView = gdxView as SurfaceView
            glView.holder.setFormat(PixelFormat.TRANSLUCENT)
            glView.setZOrderOnTop(true)
        }
        val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        fl_root.addView(gdxView, params)
        fl_root.setBackgroundColor(Color.TRANSPARENT)
    }

    fun doAction(action: String, loop: Boolean, listener: GdxAdapter.ActionCompleteListener) {
        gdxAdapter?.doAction(action, loop, listener)
    }


    override fun onPause() {
        //修复框架onPause时的崩溃问题
        //详情见https://blog.csdn.net/niubitianping/article/details/82733670
        if (activity!!.window.attributes.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
            exec.submit(forcePause)
        }
        super.onPause()
    }

    companion object {
        @JvmStatic
        fun newGdxFragment(atlasPath: String, jsonPath: String) = GdxFragment().apply {
            arguments = Bundle().apply {
                putString(EXTRA_PATH_ATLAS, atlasPath)
                putString(EXTRA_PATH_JSON, jsonPath)
            }
        }
    }

}